import Vue from "vue";
import Router from "vue-router";

Vue.use(Router);

/* Layout */
import Layout from "@/layout";

export const constantRoutes = [
  {
    path: "/login",
    component: () => import("@/views/login/index"),
    hidden: true,
  },

  {
    path: "/404",
    component: () => import("@/views/404"),
    hidden: true,
  },

  {
    path: "/",
    component: Layout,
    redirect: "/dashboard",
    children: [
      {
        path: "dashboard",
        name: "展示板",
        component: () => import("@/views/dashboard/index"),
        meta: { title: "展示板", icon: "dashboard" },
      },
    ],
  },
  {
    path: "/system",
    component: Layout,
    redirect: "/system/sys-user",
    name: "系统管理",
    meta: { title: "系统管理", icon: "el-icon-s-help" },
    children: [
      {
        path: "sys-user",
        name: "用户管理",
        component: () => import("@/views/sysuser/index"),
        meta: { title: "用户管理", icon: "table" },
      },
      {
        path: "dept",
        name: "部门管理",
        component: () => import("@/views/dept/index"),
        meta: { title: "部门管理", icon: "table" },
      },
    ],
  },
  {
    path: '/product',
    component: Layout,
    redirect: '/product',
    meta: { title: "商品管理", icon: "el-icon-s-help" },
    children: [{
      path: 'product',
      name: '商品管理',
      component: () => import('@/views/product/index'),
      meta: { title: '商品管理', icon: 'dashboard' }
    },
    {
      path: 'indexImg',
      name: '轮播图管理',
      component: () => import('@/views/indexImg/index'),
      meta: { title: '轮播图管理', icon: 'dashboard' }
    }]
  },{
    path: '/order',
    component: Layout,
    redirect: '/order',
    children: [{
      path: 'order',
      name: '订单管理',
      component: () => import('@/views/order/index'),
      meta: { title: '订单管理', icon: 'dashboard' }
    }]
  },
  // 404 page must be placed at the end !!!
  { path: "*", redirect: "/404", hidden: true },
];

const createRouter = () =>
  new Router({
    scrollBehavior: () => ({ y: 0 }),
    routes: constantRoutes,
  });

const router = createRouter();

router.beforeEach((to, from, next) => {
  if ("/login" === to.path || Vue.$cookies.get("access_token")) {
    next();
  } else {
    next("/login");
  }
});

export default router;
